﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Configuration;
using WcfContrib.Configuration;

namespace WcfContrib.Internal
{
    static class EventLogHelper
    {
        /// <summary>
        /// Write a message to the event log.
        /// </summary>
        /// <param name="message">The message to be written to the event log.</param>
        /// <param name="eventType">The event log entry type.</param>
        public static void WriteEvent(string message, EventLogEntryType eventType)
        {
            WriteEvent(Constants.DefaultEventSource, Constants.DefaultEventLogName, message, eventType);
        }

        /// <summary>
        /// Write a message to the event log.
        /// </summary>
        /// <param name="source">The event source.</param>
        /// <param name="message">The message to be written to the event log.</param>
        public static void WriteEvent(string source, string message)
        {
            WriteEvent(source, Constants.DefaultEventLogName, message);
        }

        /// <summary>
        /// Write a message to the event log.
        /// </summary>
        /// <param name="source">The event source.</param>
        /// <param name="logName">The log name.</param>
        /// <param name="message">The message to be written to the event log.</param>
        public static void WriteEvent(string source, string logName, string message)
        {
            WriteEvent(source, logName, message, EventLogEntryType.Error);
        }

        /// <summary>
        /// Write a message to the event log.
        /// </summary>
        /// <param name="source">The event source.</param>
        /// <param name="message">The message to be written to the event log.</param>
        /// <param name="eventType">The event log entry type.</param>
        public static void WriteEvent(string source, string message, EventLogEntryType eventType)
        {
            WriteEvent(source, Constants.DefaultEventLogName, message, eventType);
        }

        /// <summary>
        /// Write a message to the event log.
        /// </summary>
        /// <param name="source">The event source.</param>
        /// <param name="logName">The log name.</param>
        /// <param name="message">The message to be written to the event log.</param>
        /// <param name="eventType">The event log entry type.</param>
        public static void WriteEvent(string source, string logName, string message, EventLogEntryType eventType)
        {
            //create the event source if doesn't exist
            if (!System.Diagnostics.EventLog.SourceExists(source))
            {
                System.Diagnostics.EventLog.CreateEventSource(source, logName);
            }

            //write the event log entry
            System.Diagnostics.EventLog.WriteEntry(source, message, eventType);
        }
    }
}
